using System.Collections.Generic;
using Controls;
using Data;
using UnityEngine;

namespace UI
{
    public class BulletArea
    {
        public int LeftBlockCount => _showBlockCount;
        
        private Transform _transform;
        private List<BulletBlock> listBlocks = new List<BulletBlock>();
        private int _showBlockCount;

        
        public BulletArea(Transform transform, CriticalHitController criticalHitController)
        {
            _transform = transform;
            GameObject blockGameObject = _transform.GetChild(0).gameObject;
            listBlocks.Add(blockGameObject.GetComponent<BulletBlock>());
            int bulletCapacity = ScriptableObjectHelper.GetBulletData().bulletCapacity;
            _showBlockCount = bulletCapacity;
            for (int i = 1; i < bulletCapacity; i++)
            {
                listBlocks.Add(GameObject.Instantiate(blockGameObject, _transform).GetComponent<BulletBlock>());
            }
            
            criticalHitController.CriticalHit += CriticalHitController_CriticalHit;
        }

        public void OnInputFieldChange(int textLenDiff)
        {
            // Debug.Log($"{textLenDiff}, {_showBlockCount}, {listBlocks.Count}");
            if (textLenDiff < 0)    // 减字，加子弹
            {
                listBlocks[_showBlockCount++].Show();
            }
            else if (textLenDiff > 0)
            {
                listBlocks[--_showBlockCount].Hide();
            }
        }
        
        private void CriticalHitController_CriticalHit()
        {
            _showBlockCount = ScriptableObjectHelper.GetBulletData().bulletCapacity;
            foreach (BulletBlock bulletBlock in listBlocks)
            {
                bulletBlock.Show();
            }
        }
    }
}